//
//  DWTimerViewController.m
//  OCAPPDemo
//
//  Created by 杨冬武 on 2021/4/20.
//

#import "DWTimerViewController.h"
#import "DWWeakTimerTool.h"
#import "DWTimerProxy.h"
#import "DWRunLoopViewController.h"

@interface DWTimerViewController ()

@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, strong) dispatch_source_t gcdTimer;

@end

@implementation DWTimerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

- (void)dealloc {
    
    NSLog(@"%s", __FUNCTION__);
    if ([self.timer isValid]) {
        [self.timer invalidate];
    }
    
}

#pragma mark - GCD Timer

- (IBAction)createGCDTimer:(id)sender {
    [self createGCDTimer];
}
- (IBAction)resumeGCDTimer:(id)sender {
    dispatch_resume(self.gcdTimer);
}
- (IBAction)suspendGCDTimer:(id)sender {
    dispatch_suspend(self.gcdTimer);
}
- (IBAction)cancelGCDTimer:(id)sender {
    dispatch_source_cancel(self.gcdTimer);
}



- (void)createGCDTimer {
    //队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    //创建定时器
    self.gcdTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //设置时间
    int64_t start = 0.0;
    int64_t interval = 1.0;
    dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, start * NSEC_PER_SEC);
    dispatch_source_set_timer(self.gcdTimer, startTime, interval * NSEC_PER_SEC, 0);
    
    //设置回调
//    dispatch_source_set_event_handler(self.gcdTimer, ^{
//        NSLog(@"timer");
//    });
    
    dispatch_source_set_event_handler_f(self.gcdTimer, timerFire);
    
    //启动定时器
    //dispatch_resume(self.gcdTimer);
}

void timerFire(void *a) {
    NSLog(@"timerFire");
}


#pragma mark - NSTimer

- (IBAction)createNSTimer:(id)sender {
    [self createRunLoopTimer];
}

- (IBAction)fireNSTimer:(id)sender {
    [self.timer fire];
}

- (IBAction)invalidateNSTimer:(id)sender {
    [self.timer invalidate];
}

- (IBAction)changeFireDate:(id)sender {
    
    self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
    
}

- (void)createRunLoopTimer {
    
    //    NSTimer *timer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
    //        NSLog(@"timer");
    //    }];
    //    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
    DWTimerProxy *proxy = [DWTimerProxy proxyWithTarget:self];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:proxy selector:@selector(timerSel) userInfo:nil repeats:YES];
//    self.timer = [NSTimer timerWithTimeInterval:1 target:proxy selector:@selector(timerSel) userInfo:nil repeats:NO];
    self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    
    //公差
    //计时器不会在计划的触发日期之前触发。
    //对于重复计时器，下一个点火日期是根据原始点火日期计算的，而不考虑各个点火时间的公差，以避免漂移。
//    self.timer.tolerance = 1;
    
//    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
    //[self.timer setFireDate:<#(NSDate * _Nonnull)#>];
    
    //NSProxy直接进行消息转发, 所以这里判断是否是当前类结果返回1
    //NSLog(@"%d", [proxy isKindOfClass:[self class]]);
}

- (void)timerSel {
    NSLog(@"NSTimer");
}


@end
